package com.hellpro.lolbox.home.weight;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hellpro.lolbox.R;
import com.hellpro.lolbox.base.BaseFragment;
import com.hellpro.lolbox.bean.VideoBean;
import com.hellpro.lolbox.config.UrlConfigs;
import com.hellpro.lolbox.home.dasheng.DaSheng;
import com.hellpro.lolbox.home.hero.Hero;
import com.hellpro.lolbox.home.presenter.HomeListAdapter;
import com.hellpro.lolbox.home.presenter.HomeVideoModel;
import com.hellpro.lolbox.home.shank.Shank;
import com.hellpro.lolbox.home.view.IHomeView;
import com.hellpro.lolbox.home.xiaosmein.XiaoShuoMeiNvWeb;
import com.hellpro.lolbox.utils.UIManger;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseFragment implements IHomeView, View.OnClickListener {


    @BindView(R.id.home_pullrefresh)
    PullToRefreshListView homePullList;


    private ImageView arrowImage;
    private ImageView baikeImage;
    private Banner banner;
    private TextView dSLiving;
    private TextView xShuo;
    private TextView mVLiving;
    private ImageView zJ;
    private ImageView zX;
    private ImageView yX;
    private LinearLayout viewHead;
    private HomeVideoModel homeVideoModel;
    private HomeListAdapter homeListAdapter;

    public HomeFragment() {
        // Required empty public constructor
    }



    @Override
    public void initView() {

        homeVideoModel = new HomeVideoModel(getFActivity(), this,homePullList);
        homeListAdapter = new HomeListAdapter();
        homePullList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                homeVideoModel.VideoData(1);
            }
        });
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        viewHead = (LinearLayout) LayoutInflater.from(getActivity()).inflate(R.layout.home__head, null);
        arrowImage = (ImageView) viewHead.findViewById(R.id.home_arrowtui);
        baikeImage = (ImageView) viewHead.findViewById(R.id.home_baike);
        baikeImage.setOnClickListener(this);
        banner = (Banner) viewHead.findViewById(R.id.home_banner);
        dSLiving = (TextView) viewHead.findViewById(R.id.hera_ds_text);
        dSLiving.setOnClickListener(this);
        xShuo = (TextView) viewHead.findViewById(R.id.hera_xs_text);
        xShuo.setOnClickListener(this);
        mVLiving = (TextView) viewHead.findViewById(R.id.hera_mv_text);
        mVLiving.setOnClickListener(this);
        zJ = (ImageView) viewHead.findViewById(R.id.home_zj);
        zJ.setOnClickListener(this);
        zX = (ImageView) viewHead.findViewById(R.id.home_zx);
        zX.setOnClickListener(this);
        yX = (ImageView) viewHead.findViewById(R.id.home_yx);
        yX.setOnClickListener(this);

        homePullList.getRefreshableView().addHeaderView(viewHead);
        homeVideoModel.VideoData(1);
        homePullList.setAdapter(homeListAdapter);
        setBinner();

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home;
    }


    @Override
    public void hideProgress() {
        homePullList.postDelayed(new Runnable() {
            @Override
            public void run() {
                homePullList.onRefreshComplete();
            }
        }, 1000);

    }


    @Override
    public void setListViewAdapter(VideoBean bean) {
     homeListAdapter.setHomeList(bean);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home_zx:
                UIManger.startToActivity(getFActivity(), ZXActivity.class);
                break;
            case R.id.home_zj:
                UIManger.startToActivity(getFActivity(),HomeZhanJIActivity.class);
                break;
            case R.id.home_yx:
                UIManger.startToActivity(getFActivity(), Hero.class);
                break;
            case R.id.hera_ds_text:
                UIManger.startToActivity(getFActivity(), DaSheng.class);
                break;
            case R.id.hera_xs_text:
                Intent xiao = new Intent(getFActivity(), XiaoShuoMeiNvWeb.class);
                String lolxiaoshuo = UrlConfigs.LOLXIAOSHUO;
                xiao.putExtra("path",lolxiaoshuo);
                startActivity(xiao);
                break;
            case  R.id.hera_mv_text:
                Intent mv= new Intent(getFActivity(), XiaoShuoMeiNvWeb.class);
                String mvPath = UrlConfigs.MEINVZHIBO;
                mv.putExtra("path",mvPath);
                startActivity(mv);
                break;
            case  R.id.home_baike:
                UIManger.startToActivity(getFActivity(), Shank.class);
                break;



        }
    }

    //binner绑定数据。。
    private void setBinner(){
        List<String> images = new ArrayList<>();
        images.add(UrlConfigs.HOME_PIC);
        images.add(UrlConfigs.HOME_PIC1);
        images.add(UrlConfigs.HOME_PIC2);

   banner.setImages(images, new Banner.OnLoadImageListener() {
       @Override
       public void OnLoadImage(ImageView view, Object url) {
           view.setScaleType(ImageView.ScaleType.FIT_XY);
           Glide.with(getFActivity()).load(url).into(view);
       }
   });
        //监听
//        banner.setOnBannerClickListener(new Banner.OnBannerClickListener() {
//            @Override
//            public void OnBannerClick(View view, int position) {
//
//            }
//        });

    }


}
